import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import { Dialog } from 'quasar'

// const baseUrlTpm = CommonConfig.tpmUrl + '/v1'
// const baseUrlUpms = CommonConfig.upmsUrl + '/v1'
// const baseUrlProcess = CommonConfig.processUrl + '/v1'
// const baseUrlPlanning = CommonConfig.planningUrl + '/v1'

const baseUrlTpm = CommonConfig.tpmUrl + '/v1/minio'
// const baseUrlUpms = CommonConfig.upmsUrl + '/v1/minio'
const baseUrlProcess = CommonConfig.processUrl + '/v1/minio'
const baseUrlPlanning = CommonConfig.planningUrl + '/v1/minio'
const fileUrl = CommonConfig.fileUrl + '/v1/minio'
const datacenterUrl = CommonConfig.datacenterUrl + '/v1/minio'
const cutterUrl = CommonConfig.cutterUrl + '/v1'
const datacenter = CommonConfig.datacenterUrl + '/v1'
// 上传设备台帐模版
export function uploadTPM (query) {
  const newUrl = `${baseUrlTpm}/devices/import`

  return axiosInstance.post(newUrl, query).then(resp => {
    return Promise.resolve(resp)
  })
}

// 上传用户管理模版
export function uploadUser (query) {
  const newUrl = `${datacenterUrl}/users/import`

  return axiosInstance.post(newUrl, query).then((res) => {
    return Promise.resolve(res)
  })
}
export function exportUser ({ departmentId, role, post, searchBy, keyword, requestId }) {
  let params = {
    departmentId: departmentId || undefined,
    role: role === 'all' ? undefined : role,
    post: post === 'all' ? undefined : post,
    searchBy: searchBy || undefined,
    keyword: keyword || undefined,
    requestId
  }
  const newUrl = `${datacenter}/users:export`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
//
// 上传产品模版
export function uploadProduct (query) {
  const newUrl = `${baseUrlProcess}/products/import`

  return axiosInstance.post(newUrl, query).then((res) => {
    return Promise.resolve(res)
  })
}

// 上传工序模版
export function uploadProcess (query) {
  const newUrl = `${baseUrlProcess}/working_steps/import`

  return axiosInstance.post(newUrl, query).then((res) => {
    return Promise.resolve(res)
  })
}

// 上传车间计划模版
export function uploadPlantPlan (query) {
  const newUrl = `${baseUrlPlanning}/workshop_plannings/import`

  return axiosInstance.post(newUrl, query).then((res) => {
    return Promise.resolve(res)
  })
}
// 上传刀具台账模版
export function uploadCutter (query) {
  const newUrl = `${cutterUrl}/cutter_catalogue/minio/import`

  return axiosInstance.post(newUrl, query).then((res) => {
    return Promise.resolve(res)
  })
}

// 上传工艺路线模版
export function uploadProcessRoutes (query) {
  const newUrl = `${baseUrlProcess}/process_routes/import`

  return axiosInstance.post(newUrl, query).then((res) => {
    return Promise.resolve(res)
  })
}

// 上传文件
// feat: 文件名长度不可大于80
export function uploadFlie (cloudPath, file, requestId) {
  if (file.name.length > 80) {
    Dialog.create({
      title: `<p class="dialog-notice-title"><i class="iconfont icontubiao-28" ></i>文件名过长，操作失败</p>`,
      html: true,
      ok: {
        unelevated: true,
        label: '确认'
      }
    })
    return Promise.reject('文件名过长，操作失败')
  } else {
    let fileData = new FormData()
    fileData.append('file', file)
    // const newUrl = `${fileUrl}/upload/specify_path?cloudPath=${cloudPath}`
    const newUrl = `${fileUrl}/async/upload/specify_path?cloudPath=${cloudPath}&requestId=${requestId}`

    return axiosInstance.post(newUrl, fileData).then((res) => {
      return Promise.resolve(res)
    })
  }
}

// 获取文件服务器地址
export function getBasisUrl () {
  const newUrl = `${fileUrl}/get_basis_url`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
